package com.sneakxy.cloudbase.platform.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sneakxy.cloudbase.platform.bean.Account;
import com.sneakxy.cloudbase.platform.bean.Launcher;
import com.sneakxy.cloudbase.platform.bean.ValidAdd;
import com.sneakxy.cloudbase.platform.bean.ValidEdit;
import com.sneakxy.cloudbase.platform.bean.dto.LauncherDTO;
import com.sneakxy.cloudbase.platform.service.ILauncherService;
import com.sneakxy.cloudbase.platform.service.ILauncherSettingService;
import com.sneakxy.cloudbase.platform.utils.web.BeanHelper;
import com.sneakxy.cloudbase.platform.utils.web.DataGrid;
import com.sneakxy.cloudbase.platform.utils.web.PageSearch;
import com.sneakxy.cloudbase.platform.utils.web.Response;
import com.sneakxy.cloudbase.platform.utils.web.signature.RequireSignature;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags="自启动服务接口")
@RestController
@RequestMapping("/api/launcher/")
public class LauncherController extends BaseController {

	@Autowired
	private ILauncherService launcherService;
	
	@Autowired
	private ILauncherSettingService launcherSettingService;
	
	@ApiOperation("自启动分页列表")
	@RequireSignature
	@RequiresPermissions("sys:launcher:manager")
	@PostMapping("datagrid")
	public Response<DataGrid<LauncherDTO>> datagrid(PageSearch<String> search) {
		if(StringUtils.isBlank(search.getSortName())) {
			search.setSortName("createtime");
		}
		Page<Launcher> page = launcherService.findBySearch(search.getSearch(), search.toPageable());
		return Response.ok(DataGrid.newInstance(page, LauncherDTO::from));
	}
	
	@ApiImplicitParams({
		@ApiImplicitParam(name="name", value="启动项名称", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="module", value="启动模块名", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="memo", value="备注", dataType="String", dataTypeClass = String.class, paramType="query", required=false),
		@ApiImplicitParam(name="enable", value="是否开启：true,false", dataType="boolean", dataTypeClass = Boolean.class, paramType="query", required=true),
		@ApiImplicitParam(name="manual", value="用户是否可以手动控制开启：true,false", dataType="boolean", dataTypeClass = Boolean.class, paramType="query", required=true),
		@ApiImplicitParam(name="sort", value="", dataType="int", dataTypeClass = Integer.class, paramType="query", required=true)
	})
	@ApiOperation("添加自启动")
	@RequireSignature
	@RequiresPermissions("sys:launcher:manager:add")
	@PostMapping("add")
	public Response<LauncherDTO> add(
			@Validated(ValidAdd.class)
			@ApiIgnore
			LauncherDTO dto) {
		//去空格
		BeanHelper.trimToNull(dto);
		
		Launcher launcher = LauncherDTO.to(dto); 
		launcher = launcherService.save(launcher);
		
		return Response.ok(LauncherDTO.from(launcher));
	}
	
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="自启动ID", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="name", value="启动项名称", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="module", value="启动模块名", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="memo", value="备注", dataType="String", dataTypeClass = String.class, paramType="query", required=false),
		@ApiImplicitParam(name="enable", value="是否开启：true,false", dataType="boolean", dataTypeClass = Boolean.class, paramType="query", required=true),
		@ApiImplicitParam(name="manual", value="用户是否可以手动控制开启：true,false", dataType="boolean", dataTypeClass = Boolean.class, paramType="query", required=true),
		@ApiImplicitParam(name="sort", value="", dataType="int", dataTypeClass = Integer.class, paramType="query", required=true)
	})
	@ApiOperation("自启动项修改")
	@RequireSignature
	@RequiresPermissions("sys:launcher:manager:edit")
	@PostMapping("edit")
	public Response<LauncherDTO> edit(
			@Validated(ValidEdit.class)
			@ApiIgnore
			LauncherDTO dto) {
		//去空格
		BeanHelper.trimToNull(dto);
		
		Launcher launcher = LauncherDTO.to(dto); 
		launcher.setMemo(StringUtils.trimToEmpty(launcher.getMemo()));
		launcher.setCreatetime(null);
		
		launcher = launcherService.save(launcher);
		//如果不可以用户手动控制
		if(!launcher.isLauncherManual()) {
			//必须开启
			launcherSettingService.deleteByLid(launcher.getId());
		}
		return Response.ok(LauncherDTO.from(launcher));
	}
	
	@ApiOperation("自启动项删除")
	@RequireSignature
	@RequiresPermissions("sys:launcher:manager:remove")
	@PostMapping("remove")
	public Response<String> remove(
			@ApiParam(name="ids", value="自启动ID集合", required=true)
			@RequestParam(name="ids", required = true)
			String[] ids) {
		if(ids != null) {
			for(String id:ids) {
				launcherService.remove(id);
			}
		}
		return Response.ok();
	}
	
	@ApiOperation("账户拥有所有自启动服务")
	@RequiresAuthentication
	@PostMapping("launcherModules")
	public Response<List<String>> launcherModules() {
		Account account = (Account) getLoginAccount();
		return Response.ok(launcherService.findAllLauncherModules(account.getId()));
	}
	
	@ApiOperation("账户拥有所有自启动服务")
	@RequiresAuthentication
	@PostMapping("all")
	public Response<List<LauncherDTO>> all() {
		Account account = (Account) getLoginAccount();
		return Response.ok(LauncherDTO.from(launcherService.findAllLaunchers(account.getId())));
	}
	
	@ApiOperation("查找一个自启动服务")
	@RequireSignature
	@PostMapping("one")
	public Response<LauncherDTO> one(
			@ApiParam(name="launcherId", value="自启动ID", required=true)
			@RequestParam(name="launcherId", required = true)
			String launcherId) {
		Launcher launcher = launcherService.findOne(launcherId);
		return Response.ok(launcher, LauncherDTO::from);
	}
	
}
